//=============================================================================
// System  : MatchKit
// File    : StringPattern.cs
// Author  : Fabio Bussu  (matchkit@fabiobussu.it)
// Updated : 04/16/2013
// Note    : Copyright 2013, Fabio Bussu, All rights reserved
//
// This code is published under the Microsoft Public License (Ms-PL). A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://matchkit.codeplex.com. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// Version     Date     Who  Comments
// ============================================================================
// 1.0.0.0  04/16/2013   FB  First draft
//=============================================================================
using System;
using System.Runtime.Serialization;

namespace MatchKit.Patterns
{
	/// <summary>
	/// Provides the logic to match a specific string or word.
	/// </summary>
	/// <threadsafety static="true" instance="false" />
	/// <serialization binary="true" contract="true" />
	/// <conceptualLink target="cb785773-b793-4cc4-acc4-6688cdb22dbb" />
	/// <conceptualLink target="0a06b2a9-5f58-409a-ba62-639cfd77bca7" />
	#if DC
	[DataContract]
	#endif
	#if SZ
	[Serializable]
	#endif
	public sealed class StringPattern : BasePattern
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.Patterns.StringPattern"/> class. 
		/// </summary>
		/// <param name="value">The string to match. See <see cref="P:MatchKit.Patterns.StringPattern.Value"/>.</param>
		public StringPattern(string value)
			: this(value, true, true)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:StringPattern"/> class. 
		/// </summary>
		/// <param name="value">The string to match. See <see cref="P:MatchKit.Patterns.StringPattern.Value"/>.</param>
		/// <param name="ignoreCase">The case sensitive option. See <see cref="P:MatchKit.Patterns.StringPattern.IgnoreCase"/>.</param>
		public StringPattern(string value, bool ignoreCase)
			: this(value, ignoreCase, true)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.Patterns.StringPattern"/> class. 
		/// </summary>
		/// <param name="value">The string to match. See <see cref="P:MatchKit.Patterns.StringPattern.Value"/>.</param>
		/// <param name="ignoreCase">The case sensitive option. See <see cref="P:MatchKit.Patterns.StringPattern.IgnoreCase"/>.</param>
		/// <param name="exactWord">The option to match a word. See <see cref="P:MatchKit.Patterns.StringPattern.ExactWord"/>.</param>
		public StringPattern(string value, bool ignoreCase, bool exactWord)
		{
			Value = value;
			_ignoreCase = ignoreCase;
			_exactWord = exactWord;
		}

		private string _value;
		/// <summary>
		/// Gets or sets the string to match.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public string Value
		{
			get { return _value; }
			set
			{
				if (value == null)
					throw new ArgumentNullException("value", "Value cannot be null");
				_value = value;
			}
		}

		private bool _ignoreCase;
		/// <summary>
		/// Gets or sets the case sensitive option. Default is <b>true</b>.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public bool IgnoreCase
		{
			get { return _ignoreCase; }
			set { _ignoreCase = value; }
		}

		private bool _exactWord;
		/// <summary>
		/// Gets or sets the option to match a word, followed by any non-word character, or to 
		/// match the exact string specified with <see cref="P:MatchKit.Patterns.StringPattern.Value"/>. 
		/// Default is <b>true</b>.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public bool ExactWord
		{
			get { return _exactWord; }
			set { _exactWord = value; }
		}

		/// <inheritdoc />
		protected override Match OnMatch(MatchContext context)
		{
			if (!context.NextWith(_value, _ignoreCase))
				return Error(context);
			else
			{
				// se e' richiesta la parola esatta e il carattere successivo e' ancora una lettera, getto un errore
				if (_exactWord && context.IsLetter)
					return Error(context);
				return Success(context, MatchedString.Value);
			}
		}

		/// <inheritdoc />
		public override string HelpString
		{
			get
			{
				if (_exactWord)
					return "Exact word \"" + _value + "\"";
				else
					return "Exact string \"" + _value + "\"";
			}
		}
	}
}
